﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FySystem.Entity.Common
{
    public class PgResult<T>
    {
        /// <summary>
        /// 当前页，由前端传入
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 每页显示数量，由前端传入
        /// </summary>
        public int Perpage { get; set; }

        /// <summary>
        /// 数据总条数，
        /// </summary>
        public int totals;
        /// <summary>
        /// 总数据条数
        /// </summary>
        public int Totals{ get { return totals; } }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public List<T> Data { get; set; }

        /// <summary>
        /// 在赋值Data后，生成结果
        /// </summary>
        /// <param name="totals"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Generate(int totals, QueryITO queryITO)
        {
            try
            {
                if (queryITO.IsNullOrInvalid())
                    throw new Exception("传入参数错误~");

                PageIndex = queryITO.PageIndex;
                Perpage = queryITO.Perpage;
                this.totals = totals;
                TotalPages = (int)Math.Ceiling(totals * 1.0 / (Perpage == 0 ? 1 : Perpage));
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 如果Totals已经赋值，则用该方法
        /// </summary>
        /// <param name="queryITO"></param>
        public void Generate(QueryITO queryITO)
        {
            try
            {
                if (queryITO.IsNullOrInvalid())
                    throw new Exception("传入参数错误~");

                PageIndex = queryITO.PageIndex;
                Perpage = queryITO.Perpage;
                TotalPages = (int)Math.Ceiling(Totals * 1.0 / (Perpage == 0 ? 1 : Perpage));
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
